/*
 * LinkBoard.java
 *
 * Board to display link information, preview and go button
 *
 */

package net.kernub.client;

import com.google.gwt.core.client.GWT;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;

import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;

import com.google.gwt.user.client.ui.Composite;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.DialogBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyCodes;

import java.lang.Integer;
import java.util.ArrayList;

import net.kernub.client.core.RPC;
import net.kernub.client.core.RPCCallback;
import net.kernub.client.core.datatype.CInteger;
import net.kernub.client.core.datatype.CBoolean;
import net.kernub.client.core.UpdateHandler;

import net.kernub.client.wwwConstants;
import net.kernub.client.PageTitled;
import net.kernub.client.LabelData;
import net.kernub.client.PreviewDialogBox;

import net.kernub.client.datatype.LinkDesc;
import net.kernub.client.datatype.LabelDesc;
import net.kernub.client.datatype.LabelDescArray;


public class LinkBoard extends Composite
{
	public static final String PANEL_WIDTH = "250px";

	private int id = -1;

	private final wwwConstants constants = GWT.create( wwwConstants.class ); 

	public LinkBoard( final int id, final String name, final String url, final String desc )
	{
		this.id = id;

		final Label nameLabel = new Label( name );
		nameLabel.addStyleName( "PageText" );
		nameLabel.addStyleName( "LinkBoard_name" );
		nameLabel.setWordWrap( true );

		final Label removeAnchor = new Label( "[X]" );
		removeAnchor.addStyleName( "PageText" );
		removeAnchor.addStyleName( "LinkCursor" );
		removeAnchor.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				LinkBoard.this.removeLink();
			}
		});

		final Label editAnchor = new Label( "[E]" );
		editAnchor.addStyleName( "PageText" );
		editAnchor.addStyleName( "LinkCursor" );
		editAnchor.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				LinkBoard.this.editLink();
			}
		});

		final HorizontalPanel topRightPanel = new HorizontalPanel();
		topRightPanel.add( editAnchor );
		topRightPanel.add( removeAnchor );

		final DockPanel dockPanel = new DockPanel();
		dockPanel.setWidth( "100%" );
		dockPanel.add( nameLabel, DockPanel.CENTER );
		dockPanel.setCellHorizontalAlignment( nameLabel, DockPanel.ALIGN_CENTER );

		if( UIGlobals.instance().getLinkEditable() && UIGlobals.instance().getLinkRemovable() )
		{
			dockPanel.add( topRightPanel, DockPanel.EAST );
			dockPanel.setCellHorizontalAlignment( topRightPanel, DockPanel.ALIGN_RIGHT );
		}

		UIGlobals.instance().addUpdateHandler( new UpdateHandler() {
			public void onUpdate()
			{
				if( UIGlobals.instance().getLinkEditable() && UIGlobals.instance().getLinkRemovable() )
				{
					dockPanel.add( topRightPanel, DockPanel.EAST );
					dockPanel.setCellHorizontalAlignment( topRightPanel, DockPanel.ALIGN_RIGHT );
				}
				else
				{
					dockPanel.remove( topRightPanel );
				}
			}
		});

		final Label descLabel = new Label( desc );
		descLabel.addStyleName( "PageText" );
		descLabel.addStyleName( "LinkBoard_desc" );
		descLabel.setWordWrap( true );

		final Hyperlink previewLink = new Hyperlink();
		previewLink.setText( constants.linkPreview() );
		previewLink.addStyleName( "PageText" );
		previewLink.addStyleName( "LinkCursor" );
		previewLink.addStyleName( "LinkBoard_buttons" );
		previewLink.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				final PreviewDialogBox preview = new PreviewDialogBox( name, url );
			}
		});

		final Hyperlink goLink = new Hyperlink();
		goLink.setText( constants.linkGo() );
		goLink.addStyleName( "PageText" );
		goLink.addStyleName( "LinkCursor" );
		goLink.addStyleName( "LinkBoard_buttons" );
		goLink.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				Window.open( url, name, "" );
			}
		});

		final HorizontalPanel linkPanel = new HorizontalPanel();
		linkPanel.setWidth( "100%" );
		linkPanel.add( previewLink );
		linkPanel.add( goLink );
		linkPanel.setCellHorizontalAlignment( previewLink, HorizontalPanel.ALIGN_LEFT );
		linkPanel.setCellHorizontalAlignment( goLink, HorizontalPanel.ALIGN_RIGHT );

		final VerticalPanel panel = new VerticalPanel();
		panel.setWidth( "100%" );
		panel.add( dockPanel );
		panel.add( descLabel );
		panel.add( linkPanel );

		/*
		 * NOTE DecoratorPanel: if element has CSS display: inline, unable to set the size, except in the CSS file.
		 */
		final DecoratorPanel decoPanel = new DecoratorPanel();
		decoPanel.addStyleDependentName( "LinkBoard" );
		decoPanel.setWidth( PANEL_WIDTH );
		decoPanel.setWidget( panel );

		// used to have display inline set
		final HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.addStyleName( "LinkBoard" );
		hpanel.add( decoPanel );

		this.initWidget( hpanel );
	}

	private void removeLink()
	{
		RPC.call( "site/Link", "remove", this.id, new RPCCallback<CBoolean>() {
			public void onCallback( CBoolean r )
			{
				if( r.get() )
				{
					LinkBoard.this.setVisible( false );
				}
				else
				{
					Window.alert( constants.linkRemoveError() );
				}
			}
		});
	}

	private void editLink()
	{
		www.instance().setPage( www.PAGE_ADDEDIT_LINK );

		UserPageTitledAddEditLink page = (UserPageTitledAddEditLink) www.instance().getPage();
		page.loadLink( this.id );
	}

};


